package com.tfswx.jcyw.ruleenginedesigner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tfswx.ruleengine.compute.bean.GzDataDTO;
import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.compute.util.CommonUtil;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtGzflMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtGzfl;
import com.tfswx.jcyw.ruleenginedesigner.dto.gzflgl.*;
import com.tfswx.jcyw.ruleenginedesigner.service.GzflService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/5/11
 */
@Service
public class GzflServiceImpl implements GzflService {
    @Resource
    private GzyqXtGzflMapper gzyqXtGzflMapper;

    @Override
    public List<GzflListByXmbmQueryOutputDTO> queryGzflListByXmbm(GzflListByXmbmQueryInputDTO gzflListByXmbmQueryInputDTO) {
        GzyqXtGzfl gzyqXtGzfl = new GzyqXtGzfl();
        BeanUtils.copyProperties(gzflListByXmbmQueryInputDTO, gzyqXtGzfl);
        return gzyqXtGzflMapper.queryGzflListByXmbm(gzyqXtGzfl);
    }

    @Override
    public String addGzfl(GzflAddInputDTO gzflAddInputDTO) {
        GzyqXtGzfl gzyqXtGzfl = new GzyqXtGzfl();
        BeanUtils.copyProperties(gzflAddInputDTO, gzyqXtGzfl);
        gzyqXtGzfl.setGzflbm(CommonUtil.getUuid());
        gzyqXtGzfl.setSfsc("N");
        gzyqXtGzfl.setCjsj(new Date());
        gzyqXtGzfl.setZhxgsj(new Date());
        int insert = gzyqXtGzflMapper.insert(gzyqXtGzfl);
        if (insert == 1) {
            return gzyqXtGzfl.getGzflbm();
        }
        throw new AppException("添加规则分类失败");
    }

    @Override
    public boolean updateGzfl(GzflUpdateInputDTO gzflUpdateInputDTO) {
        GzyqXtGzfl gzyqXtGzfl = new GzyqXtGzfl();
        BeanUtils.copyProperties(gzflUpdateInputDTO, gzyqXtGzfl);
        gzyqXtGzfl.setZhxgsj(new Date());
        int insert = gzyqXtGzflMapper.update(gzyqXtGzfl);
        if (insert == 1) {
            return true;
        }
        throw new AppException("修改规则分类失败");
    }

    @Override
    public boolean deleteGzfl(GzflDeleteInputDTO gzflDeleteInputDTO) {
        GzyqXtGzfl gzyqXtGzfl = new GzyqXtGzfl();
        BeanUtils.copyProperties(gzflDeleteInputDTO, gzyqXtGzfl);
        gzyqXtGzfl.setZhxgsj(new Date());
        int insert = gzyqXtGzflMapper.delete(gzyqXtGzfl);
        if (insert == 1) {
            return true;
        }
        throw new AppException("删除规则分类失败");
    }

    @Override
    public GzflByGzflbmQueryOutputDTO queryGzflByGzflbm(GzflByGzflbmQueryInputDTO gzflByGzflbmQueryInputDTO) {
        GzyqXtGzfl gzyqXtGzfl = BeanUtil.copyProperties(gzflByGzflbmQueryInputDTO, GzyqXtGzfl.class);
        gzyqXtGzfl = gzyqXtGzflMapper.queryGzflByGzflbm(gzyqXtGzfl);
        if (gzyqXtGzfl == null) {
            return null;
        }
        return BeanUtil.copyProperties(gzyqXtGzfl, GzflByGzflbmQueryOutputDTO.class);
    }

    @Override
    public List<GzflbmAndGzbmQueryOutputDTO> queryGzflbmAndGzbm(GzflbmAndGzbmQueryInputDTO gzflbmAndGzbmQueryInputDTO) {
        GzyqXtGzfl gzyqXtGzfl = BeanUtil.copyProperties(gzflbmAndGzbmQueryInputDTO, GzyqXtGzfl.class);
        return gzyqXtGzflMapper.queryGzflbmAndGzbm(gzyqXtGzfl);
    }

    @Override
    public int qkGzfl(GzflQkInputDTO gzflQkInputDTO) {
        List<String> xmbmList = gzflQkInputDTO.getXmbmList();
        if (xmbmList == null || xmbmList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < xmbmList.size(); i += 500) {
            int end = i + 500;
            if (end > xmbmList.size()) {
                end = xmbmList.size();
            }
            List<String> strings = xmbmList.subList(i, end);
            count += gzyqXtGzflMapper.qkGzfl(strings);
        }
        return count;
    }

    @Override
    public int importGzflList(List<GzDataDTO.Gzfl> collect) {
        if (collect == null || collect.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < collect.size(); i += 500) {
            int end = i + 500;
            if (end > collect.size()) {
                end = collect.size();
            }
            List<GzDataDTO.Gzfl> strings = collect.subList(i, end);
            count += gzyqXtGzflMapper.importGzflList(strings);
        }
        return count;
    }
}
